/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datos;
import dominio.user;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alejandro
 */
public class DAOuser {

    private Bd basedeDatos = new Bd();

    public boolean insertUser(user usuario) {
        try {
            basedeDatos.ejecutar("INSERT INTO USERS(`email`,`nombre`,`password`) VALUES('" + usuario.getEmail() + "','" + usuario.getNombre() + "','" + usuario.getPassword() + "')");
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DAOuser.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public List<user> getAll() {
        try {
            List<user> users = new ArrayList<user>();
            ResultSet rs = basedeDatos.obtenerResultSet("SELECT * FROM USERS");
            while (rs.next()) {
                users.add(new user(rs.getString("nombre"), rs.getString("email"), rs.getString("password")));
            }
            return users;
        } catch (SQLException ex) {
            Logger.getLogger(DAOuser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    public user searchUser(user user)
    {
        for(user u:getAll())
        {
            if(u.getEmail().equals(user.getEmail()) && u.getPassword().equals(user.getPassword()))
                return u;
        }
        return null;
    }
}
